<?php
namespace app\tadmin\controller\category;
use app\tadmin\controller\Controller as BaseController;
use app\tadmin\model\Category as CategoryModel;
use app\common\model\ArticleRecord as ArticleRecordModel;
use app\common\model\Tag as TagModel;
use think\facade\Request;
/**
 * 文档-录像
 * Class Index
 * @package app\tadmin\controller
 */
class Record extends BaseController{

    public function index(){
        $category_id = input('category_id');
        $category = CategoryModel::get($category_id);
        $list = ArticleRecordModel::field("doc.*,GROUP_CONCAT(tag.tag_name SEPARATOR ',') AS tags")
            ->alias('doc')
            ->join('__ARTICLE_RECORD_TAG__ doctag' , 'doctag.article_record_id=doc.id', 'LEFT')
            ->join('__TAG__ tag' , 'tag.id=doctag.tag_id', 'LEFT')
            ->where('doc.category_id' , $category_id)
            ->order('doc.id desc')
            ->group('doc.id')
            ->paginate(15);
        //print_r(ArticleVedioModel::getLastSql());
        return $this->fetch('index' , compact('category' , 'list'));
    }

    public function add(){
        if (!$this->request->isAjax()) {
            $category_id = input('category_id');
            $category = CategoryModel::get($category_id);
            return $this->fetch('add' , compact('category'));
        }
        $model = new ArticleRecordModel();
        $docData = $this->postData('doc');
        if ($model->add($docData)) {
            return $this->renderSuccess('添加成功', url('category.record/index', ['category_id'=>$docData['category_id']]));
        }
        $error = $model->getError() ?: '添加失败';
        return $this->renderError($error);
    }

    public function edit(){
        if (!$this->request->isAjax()) {
            $category_id = input('category_id');
            $category = CategoryModel::get($category_id);
            $info = ArticleRecordModel::get(input('id'));
            $info->tags = TagModel::getTags($info['id'] , 'record');
            //print_r($info);
            return $this->fetch('edit' , compact('category', 'info'));
        }
        $docData = $this->postData('doc');
        $model = ArticleRecordModel::get($docData['id']);
        if ($model->edit($docData)) {
            return $this->renderSuccess('编辑成功', url('category.record/index', ['category_id'=>$docData['category_id']]));
        }
        $error = $model->getError() ?: '编辑失败';
        return $this->renderError($error);
    }

    public function delete($id){
        $model = ArticleRecordModel::get($id);
        if (!$model->del()) {
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
    
    public function change(){
        $id = input('post.id');
        $field = input('post.field');
        $value = input('post.value');
        $model = ArticleRecordModel::get($id);
        if (!$model->changeField($field , $value)) {
            return $this->renderError('修改失败');
        }
        return $this->renderSuccess('修改成功');
    }

}